﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using TencentCloud.Common;
//using TencentCloud.Common.Profile;
//using TencentCloud.Sms.V20210111;
//using TencentCloud.Sms.V20210111.Models;

namespace Help.GLB.Global
{
    public static class PhoneHelper
    {
        public static bool Send(string templateId, string[] templateParams, string[] receivers,
            out string response)
        {
#if FakeSMS
            for (int i = 0; i < receivers.Length; i++)
            {
                Debug.WriteLine($"发送{receivers[i]}给短信，templateId={templateId}，包含参数：");
                for (int j = 0; j < templateParams.Length; j++)
                {
                    Debug.WriteLine($"参数{j}：{templateParams[j]}");
                }
            }
            Debug.WriteLine("-----------");
            response = "发送成功";
            return true;
#else
            try
            {
                SmsClient client = new SmsClient(new Credential
                {
                    SecretId = Config.SMS.SecretId,
                    SecretKey = Config.SMS.SecretKey
                },
                Config.SMS.Region,
                new ClientProfile(ClientProfile.SIGN_TC3SHA256)
                {
                    HttpProfile = new HttpProfile()
                });

                SendSmsRequest request = new SendSmsRequest
                {
                    SmsSdkAppId = Config.SMS.SdkAppId,
                    SignName = "一起学编程网",
                    TemplateId = templateId,
                    TemplateParamSet = templateParams,
                    PhoneNumberSet = receivers
                };

                response = AbstractModel.ToJsonString(client.SendSmsSync(request));
                return true;
            }
            catch (Exception e)
            {
                response = e.ToString();
                return false;
            }       
#endif
        }

        public static string Serialize(string[] templateParams)
        {
            return string.Join(",", templateParams);
        }
    }


}
